<?php
require "./types.php";
require "./utility.php";

if (check_keys($_GET, "keyword")) {
    $keyword = $_GET["keyword"];
    $conn = connect_mysql();

    $query_sql = "SELECT * FROM Book WHERE title LIKE '%$keyword%';";
    $query = $conn->query($query_sql);

    $books = [];

    while ($book_data =  $query->fetch_row()) {
        $book = Book::fromArray($book_data);

        $books[] = $book;
    }

    header("Content-Type: application/json");

    echo json_encode($books, JSON_UNESCAPED_UNICODE);

    $query->close();
    $conn->close();
} else {
    http_response_code(404);
}

?>